package com.cn.por.util.freemarker;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.StringTypeHandler;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.apache.ibatis.type.TypeReference;

import com.cn.por.auto.dto.FunctionMessage;
import com.cn.por.auto.enums.FunctionName;

public class StringFormaterUtil {
	
	static TypeHandlerRegistry register = new TypeHandlerRegistry();
	
	public static String columnToProperty (String tableName) {
		String property = "";
		if(!tableName.contains("_")) {
			property = tableName.toLowerCase();
		}else {
			List<String> tableNameList =Arrays.asList(tableName.toLowerCase().split("_"));
			for(String tn : tableNameList) {
				property+=captureName(tn);
			}
		}
		return property;
	}
	
	/**
	 * 首字母大写，进行字母的ascii编码前移
	 * @param name
	 * @return
	 */
	public static String captureName(String name) {
		StringBuilder result = new StringBuilder();
		char firstChar = name.charAt(0);
		if (Character.isLetter(firstChar)) {
			result.append(Character.toUpperCase(firstChar));
			result.append(name.substring(1));
		} else {
			result.append(name);
		}
		return result.toString();
	}
	
	/**
	 * 首字母小写，进行字母的ascii编码前移
	 * @param name
	 * @return
	 */
	public static String lowerCaptureName(String name) {
		StringBuilder result = new StringBuilder();
		char firstChar = name.charAt(0);
		if (Character.isLetter(firstChar)) {
			result.append(Character.toLowerCase(firstChar));
			result.append(name.substring(1));
		} else {
			result.append(name);
		}
		return result.toString();
	}
	
	public static List<FunctionMessage> getFunctionMsg(){
		List<FunctionMessage> functionMsgs = new ArrayList<FunctionMessage>();
		for(FunctionName functionName : FunctionName.values()) {
			
		}
		/**
		 * 根据id获取
		 */
		FunctionMessage functionMessage = new FunctionMessage();
		functionMessage.setFunctionName(FunctionName.getById.name());
		
		return functionMsgs;
	}
	/**
	 * 根据传入的数据库字段类型转换为java类型，依赖于Mybatis框架
	 * @param jdbcType
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String typeTransformer(JdbcType jdbcType) {
		TypeHandler<?> typeHandler =register.getTypeHandler(jdbcType);
		BaseTypeHandler tr = (BaseTypeHandler) typeHandler;
		Type  type = tr.getRawType();
		String classes = type.getTypeName();
		String classStr =  classes.substring(classes.lastIndexOf(".")+1,classes.length());
		return classStr;
	}
}
